/*
 * Copyright 2012 Dominik.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cz.muni.fi.PA165.musiclibraryweb.author;

import cz.muni.fi.PA165.musiclibraryweb.HomePage;
import cz.muni.fi.PA165.musiclibrarybackend.dto.AuthorDTO;
import cz.muni.fi.PA165.musiclibraryapi.services.AuthorService;
import java.io.Serializable;
import org.apache.wicket.authroles.authorization.strategies.role.annotations.AuthorizeInstantiation;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


@AuthorizeInstantiation({"ADMIN"})
public class AddAuthorPage extends HomePage implements Serializable {

    @SpringBean
    private AuthorService service;
    private AuthorDTO author;

    public AddAuthorPage(final PageParameters parameters) {
        this(parameters, null);
    }

    public AddAuthorPage(final PageParameters parameters, AuthorDTO adto) {
        super(parameters);
        add(new BookmarkablePageLink("authorView", AuthorPage.class));
        add(new BookmarkablePageLink("authorAdd", AddAuthorPage.class));

        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        service = context.getBean(AuthorService.class);

        if (adto == null) {
            author = new AuthorDTO();
        } else {
            author = adto;
        }
        final TextField<String> nameField = new TextField("name", new PropertyModel<String>(author, "name"));
        Form<?> form = new Form("form") {
            @Override
            protected void onSubmit() {
                if (nameField.getConvertedInput() == null || nameField.getConvertedInput().equals("")) {
                    setResponsePage(new AuthorPage(parameters, new Label("error", "Author name should be filled.")));
                } else {
                    if (author.getId() != null) {
                        author.setName(nameField.getValue());
                        try {
                            service.updateAuthor(author);
                            setResponsePage(AuthorPage.class);
                        } catch (Exception exception) {
                            setResponsePage(new AuthorPage(parameters,
                                    new Label("error", "Database error!")));
                        }
                    } else {
                        try {
                            service.createAuthor(author);
                            setResponsePage(AuthorPage.class);
                        } catch (Exception exception) {
                            setResponsePage(new AuthorPage(parameters,
                                    new Label("error", "Database error!")));
                        }
                    }
                }
            }
        };
        form.add(nameField);
        add(form);

    }
}
